package pref.junx.flybird.activity.register;

import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import pref.junx.flybird.R;
import pref.junx.flybird.activity.login.LoginActivity;
import pref.junx.flybird.activity.main.NewMainActivity;
import pref.junx.flybird.base.BaseActicvitylhy;
import pref.junx.flybird.entity.Register;
import pref.junx.flybird.tools.JumpUtil;


/**
 * Created by 半生瓜 on 2017/8/7.
 */

public class RegisterActvity extends BaseActicvitylhy<RegisterPresenter, RegisterModel> implements RegisterConteact.View {

    private Toolbar toolbar;

    private EditText reg_name_et;

    private EditText reg_phone_et;

    private EditText reg_psw;

    private EditText reg_psw2;

    private EditText reg_phone;

    private EditText reg_email;

    private Button reg_sure_bt;


    @Override
    public int getLayoutId() {
        return R.layout.register_layout;
    }

    @Override
    public void initview() {
        reg_phone=(EditText) findViewById(R.id.reg_phone);
        reg_email=(EditText) findViewById(R.id.reg_email);
        reg_name_et = (EditText) findViewById(R.id.reg_name_et);
        reg_phone_et = (EditText) findViewById(R.id.reg_nub_et);
        reg_psw = (EditText) findViewById(R.id.reg_psw);
        reg_psw2 = (EditText) findViewById(R.id.reg_psw2);
        toolbar = (Toolbar) findViewById(R.id.register_toolbar);
        reg_sure_bt = (Button) findViewById(R.id.reg_sure_bt);
        reg_sure_bt.setOnClickListener(this);
        setSupportActionBar(toolbar);

        //在toolbar上设置返回按钮

    }

    @Override
    public void processClick(View view) {

        switch (view.getId()) {
            case R.id.reg_sure_bt:
                sentenced_to_empty();
        }
    }


    @Override
    public void init() {
    }

    private void sentenced_to_empty() {
        if (reg_name_et.getText().length() == 0) {
            reg_name_et.requestFocus();
            reg_name_et.setError("请输入姓名");
            return;
        }
        if (reg_phone_et.getText().length() == 0) {
            reg_phone_et.requestFocus();
            reg_phone_et.setError("请输入学号");
            return;
        }

        if (reg_psw.getText().length() == 0) {
            reg_psw.requestFocus();
            reg_psw.setError("请输入密码");
            return;
        }

        if (reg_psw2.getText().length() == 0) {
            reg_psw2.requestFocus();
            reg_psw2.setError("请再次输入密码");
            return;
        }
        if (!reg_psw.getText().toString().trim().equals(reg_psw2.getText().toString().trim())) {
            reg_psw2.requestFocus();
            reg_psw2.setError("两次输入密码不一致");
            return;
        }
        //todo 注册
        Register register=new Register();
        register.setPhone(reg_phone.getText().toString().trim());
        register.setUser_nick(reg_name_et.getText().toString().trim());
        register.setUser_psd(reg_psw.getText().toString().trim());
        register.setUser_name(reg_phone_et.getText().toString().trim());
        register.setEamil(reg_email.getText().toString().trim());
        presenter.doreg(register);
    }


    @Override
    public void success() {

        t.show_l("注册成功");
        spTools.setBoolean("remember", false);
        spTools.setString("username", reg_phone_et.getText().toString().trim());
        spTools.setString("password", "");
        finish();
        JumpUtil.to(c, NewMainActivity.class);
    }

    @Override
    public void result() {
        reg_sure_bt.requestFocus();
        reg_sure_bt.setError("注册失败");
             //todo 登入

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        JumpUtil.to(c, LoginActivity.class);
            return false;

    }
}


